﻿using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Layout;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using TIMA.DevTeam;
using TIMA.Properties;

namespace TIMA
{
    public partial class Similars : XForm
    {
        int ProductId;
        public Similars(int ProductId)
        {
            this.ProductId = ProductId;
            InitializeComponent();
         
            this.Text = "Similar Fabrics of " + ProductId.ToString();
        } 

        LayoutViewField lvfPhoto;
        int sz = 150;
        Color color1 = Color.Red;
        Color color2 = Color.Transparent;

        private void FindSimilar(object sender, EventArgs e)
        {
            int productId = lv.GetFocusedRowCellValue("ProductId").ToIntNN();
            if (productId > 0)
                AppSession.MainForm.ShowMdiChildForm(typeof(Similars), productId);
        }



        void InitLV()
        {
            DataTable dt = FabricManager.GetSimilars(ProductId);
                       
            //GridMenu gm = GridManager.GetManager(lv).GridMenu;
            //DXMenuItem mi = gm.AddMenu("Find similar fabrics", FindSimilar, false);
            //mi.Image = Properties.Resources.miCopy;             

            gc.DataSource = dt;

            lv.PopulateColumns();             

            lv.OptionsBehavior.Editable = false;

            lv.Columns["Middle"].Caption = "Scn";
            lv.Columns["Middle"].ColumnEdit = new RepositoryItemPictureEdit();
            lvfPhoto = lv.Columns["Middle"].LayoutViewField;
            lvfPhoto.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
            lvfPhoto.MinSize = lvfPhoto.MaxSize = new Size(sz, sz);  //140x140 idi

            lv.CardMinSize = new Size(sz, sz);
            lv.OptionsMultiRecordMode.MultiRowScrollBarOrientation = DevExpress.XtraGrid.Views.Layout.ScrollBarOrientation.Vertical;
            lv.OptionsView.ViewMode = DevExpress.XtraGrid.Views.Layout.LayoutViewMode.MultiRow;
            lv.OptionsView.ShowCardCaption = false;
            lv.OptionsView.ShowCardExpandButton = false;
            lv.OptionsBehavior.UseTabKey = true;
            lv.OptionsBehavior.KeepFocusedRowOnUpdate = true;

            lv.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;

            foreach (LayoutViewField lvf in lv.TemplateCard.Items)
            {
                lvf.Padding = new DevExpress.XtraLayout.Utils.Padding(4, 4, 0, 0);
                lvf.EditorPreferredWidth = sz;
            }
            lvfPhoto.Padding = new DevExpress.XtraLayout.Utils.Padding(2, 2, 0, 0);

            lv.Columns["ProductId"].Caption = "F Id";
            lv.Columns["FileName"].Visible = false;
            //lv.Columns["Hue"].Visible = false;

            foreach (LayoutViewColumn lvc in lv.Columns)
            {
                lvc.OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
                lvc.OptionsFilter.AllowFilter = false;
            }

            gc.MouseClick+=gc_MouseClick;
            btnFindSimilars.Glyph = Resources.wizard_16x16;
            btnShowFabric.Glyph = Resources.zoom_16x16;
            btnFindSimilars.ItemClick +=btnFindSimilars_ItemClick;
            btnShowFabric.ItemClick +=btnShowFabric_ItemClick;
        }

        private void Similars_Load(object sender, EventArgs e)
        {
            InitLV();
        }

        private void btnFindSimilars_ItemClick(object sender, EventArgs e)
        {
            int productId = lv.GetFocusedRowCellValue("ProductId").ToIntNN();
            if (productId > 0)
                AppSession.MainForm.ShowMdiChildForm(typeof(Similars), productId);

        }

        void btnShowFabric_ItemClick(object sender, EventArgs e)
        {
            int productId = lv.GetFocusedRowCellValue("ProductId").ToIntNN();
            if (productId > 0)
            {
                AppSession.MainForm.ShowMdiChildForm(typeof(FabricViewer), productId);
            }
        }

        private void gc_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right) return;
            
            popup.ShowPopup(gc.PointToScreen(e.Location));
        }
    }
}